package com.xuelang.pipeline.domain.resource.service.impl;

import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.resource.dto.DictQuery;
import com.xuelang.pipeline.domain.resource.Dict;
import com.xuelang.pipeline.domain.resource.repository.DictRepository;
import com.xuelang.pipeline.domain.resource.service.DictDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 环境领域服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class DictDomainServiceImpl implements DictDomainService {

    @Autowired
    private DictRepository dictRepository;

    @Override
    public PageRecord<Dict> pageList(long currentPage, long pageSize, DictQuery dictQuery) {
        return dictRepository.pageList(currentPage, pageSize, dictQuery);
    }

    @Override
    public List<Dict> list(DictQuery dictQuery) {
        return dictRepository.list(dictQuery);
    }


    @Override
    public Boolean save(Dict dict) {
        if(existsCode(dict.getDictType(), dict.getDictCode())) {
            throw new BizException("dictCode已经存在于该dictType下");
        }
        return dictRepository.save(dict);
    }

    @Override
    public Boolean update(Dict dict) {
         // 更新时，名称不能重复，摒弃当前环境的名称。
        if(existsCode(dict.getDictType(), dict.getDictCode())) {
            throw new BizException("dictCode已经存在于该dictType下");
        }
        return dictRepository.update(dict);
    }

    @Override
    public Boolean delete(Long id) {
        return dictRepository.delete(id);
    }

    @Override
    public Dict query(Long id) {
        return dictRepository.query(id);
    }

    @Override
    public Boolean existsCode(String dictType, String dictCode) {
        return dictRepository.existsCode(dictType, dictCode);
    }

}
